package org.apogeescience.connector;

import java.sql.SQLException;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.apogeescience.annotation.NamedQuery;
import org.apogeescience.config.DatabaseConfiguration;
import org.apogeescience.connector.databases.DatabaseFactory;
import org.apogeescience.connector.databases.IDatabase;

/**
 * Database driver executes all queries
 * @author Isaac
 *
 */
public class DatabaseDriver {

	private IDatabase database;

	public static DatabaseDriver driver;

	private DatabaseDriver() {
		try {			

			this.database = DatabaseFactory.getDatabase(new DatabaseConfiguration());
			
		} catch (Exception ex) {

		}
	}

	/**
	 * Gets the database
	 * @return
	 */
	private IDatabase getDatabase() {
		return this.database;
	}

	/**
	 * gets the current instance of the database driver
	 * @return
	 */
	private static DatabaseDriver getInstance() {
		if (driver == null)
			driver = new DatabaseDriver();
		return driver;
	}

	/**
	 * Executes a query
	 * @param query
	 * @return
	 * @throws SQLException
	 */
	public static Map<String, List<Object>> executeQuery(String query) throws SQLException {
		return executeQuery(new StringBuilder(query));
	}

	public static Map<String, List<Object>> executeQuery(StringBuilder query) throws SQLException {
		return getInstance().getDatabase().executeQuery(query);
	}

	public static String generateQuery(String tableName, NamedQuery namedQuery, Hashtable<String, Object> parameters) {
		return getInstance().getDatabase().generateQuery(tableName, namedQuery, parameters);
	}
}
